﻿namespace Rally_MvvmC.Library
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Data.Entity;
    using Newtonsoft.Json;

    public class ViewModelBase : IViewModel
    {
        public ViewModelBase()
        {
        }

        public virtual object GetEntity()
        {

            throw new NotImplementedException();
        }

        public virtual Collection<IViewModel> GetModified()
        {
            throw new NotImplementedException();
        }

        public virtual int Id { get; set; }

        [JsonIgnore]
        public virtual int CreatedByUserId { get; set; }
        
        [JsonIgnore]
        public virtual int UpdatedByUserId { get; set; }

        [JsonIgnore]
        public virtual DateTime CreatedDate { get; set; }
        
        [JsonIgnore]
        public virtual DateTime UpdatedDate { get; set; }

        public virtual bool IsDirty
        {
            get
            {
                return
                    _IsDirty;
            }
            set
            {
                if(!value)
                {

                }
                _IsDirty = value;
            }
        }

        private bool _IsDirty { get; set; }
        
        public virtual bool ForceSaveAsNew { get; set; }
        
        public virtual bool ScheduleForDelete { get; set; }
        
        protected DbContext DbContext { get; set; }
    }

    public class ViewModelBase<DataStoreType> : ViewModelBase
    where DataStoreType : IDataStore
    {
        private DataStoreType dataStore { get; set; }

        public override object GetEntity()
        {


            throw new NotImplementedException();
        }
        public override Collection<IViewModel> GetModified()
        {
            throw new NotImplementedException();
        }
        public ViewModelBase()
        {
        }

        [JsonIgnore]
        public DataStoreType DataStore 
        {
            get
            {
                if (this.dataStore == null)
                {
                    return this.dataStore;
                    this.dataStore = this.createDataStore();
                }

                if (this.dataStore == null)
                {
                    throw new NullReferenceException("dataStore not initialized");
                }
                else
                {
                    return this.dataStore;
                }
            }
            set
            {
                this.dataStore = value;
            }
        }

        protected virtual Func<DataStoreType> createDataStore
        {
            get
            {
                throw new NotImplementedException();
            }
        }

    }

    public class ViewModelBase<DataStoreType, EntityClass> : ViewModelBase<DataStoreType>
        where DataStoreType : IDataStore
        where EntityClass : class
    {
        public ViewModelBase()
        {
        }


        public ViewModelBase(DbContext dataContext, IEnumerable<EntityClass> entitiesCollection, EntityClass entity)
        {
            this.DbContext = dataContext;
            this.Entity = entity;
        }

        [JsonIgnore]
        public virtual EntityClass Entity { get;  set; }

        public virtual Collection<IViewModel> GetModified()
        {
            var returnVal = new System.Collections.ObjectModel.Collection<IViewModel>();

            return returnVal;
        }

        public override object GetEntity()
        {
            return this.Entity;
        }
    }
}
